<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 16:01
 */

namespace app\service;


use app\common\model\DynamicComment;
use app\common\model\DynamicThumb;
use think\Db;
use think\Exception;
use think\Model;

/**
 * Class DynamicService
 * @package app\service
 */
class DynamicService extends BaseService
{
    public $dynamicModel;
    public $dynamicCommentModel;
    public $dynamicThumbModel;

    public function __construct()
    {
        $this->dynamicModel = new \app\common\model\Dynamic();
        $this->dynamicCommentModel = new DynamicComment();
        $this->dynamicThumbModel = new DynamicThumb();
    }


    /**
     * 新增/编辑
     * @param $add
     * @param null $id
     * @return false|int
     */
    public function add($data, $where=[])
    {
        if (!$where) {
            return $this->dynamicModel->insertGetId($data);
        } else {
            return $this->dynamicModel->save($data, $where);
        }
    }

    /**
     * 分页查询
     * @param $where
     * @param int $page
     * @param int $size
     * @return array|bool
     */
    public function queryPage($where, $page = 1, $size = 20, $field = "*", $order = "a.create_time desc", $is_comment = false, $uid = 0)
    {

        try {
            $data = $this->dynamicModel
                ->alias('a')
                ->join('zkhb_user b', 'a.uid=b.id', 'left')
                ->field($field)
                ->where($where)
                ->order($order)
                ->page($page, $size)
                ->select();
            if ($data) {
                $data = $data->toArray();
                foreach ($data as $key => $val) {
                    if (isset($val['images']) && $val['images']) {
                        $images = json_decode($val['images'], true);
                        foreach ($images as $k => $v) {
                            $images[$k] = tomedia($v);
                        }
                        $data[$key]['images'] = $images;
                    }
                    if (isset($val['video']) && $val['video']) {
                        $data[$key]['video'] = tomedia($val['video']);
                    }
                    if ($is_comment) {
                        $where = [
                            'a.a_id' => $val['id']
                        ];
                        $comment = DynamicCommentService::S()->queryPage($where, 1, 4, 'a.content,b.nickname');
                        $data[$key]['comment'] = $comment;
                    };
                    if ($uid) {
                        $data[$key]['is_thumb'] = DynamicThumb::getOne(['uid' => $uid, 'a_id' => $val['id']]);
                    }
                    $data[$key]['thumb_count'] = $this->dynamicThumbModel->where('a_id',$val['id'])->count();

                }
            } else {
                $data = [];
            }
            return $data;
        } catch (Exception $ex) {
            LogService::S()->writeLog('Exception', $ex->getMessage());
            return false;
        }
    }


    /**
     * 总条数
     * @param $where
     * @return bool|int|string
     */
    public function queryCount($where)
    {
        try {
            $count = $this->dynamicModel
                ->alias('a')
                ->join('zkhb_user b', 'a.uid=b.id', 'left')
                ->where($where)
                ->count();
            return $count ? $count : 0;
        } catch (Exception $ex) {
            LogService::S()->writeLog('Exception', $ex->getMessage());
            return false;
        }
    }

}